
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
  plugins:[createPersistedState()],
  state: {
    token:'',
    userInfo:{}
  },
  getters: {
  },
  mutations: {
    updateToken(state,newtoken){
      state.token = newtoken
    },
    updateUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    //2.定义发送请求,获取用户信息的函数
    async initUserInfo(context){
     const {data:res} = await axios.get('/my/userinfo',{
      headers:{
        Authorization:context.state.token
      } 
      })
      if (res.code === 0) {
        context.commit('updateUserInfo', res.data)
      }
    }
  },
  modules: {
  }
})
